// pages/unpaid/unpaid.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.Imageurl,
    isloadcomplete:true,

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.getGoodsOrderDetails(options.orderId)
    this.setData({
      orderid:options.orderId
    })
    
  },
  godetails(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/details/details?goodsid='+e.currentTarget.dataset.goodsid,
    })
  },
//支付剩余时间
countDown() {
  var t = this

  // var starttime = t.data.goods.createDate

  // var start = new Date(starttime).getTime()
  // var endTime = start + 10 * 60000

  var date = new Date(); //现在时间
  var now = date.getTime(); //现在时间戳

  var allTime = t.data.goods.cancelTime - now
  var m, s;
  if (allTime > 0) {
    m = Math.floor(allTime / 1000 / 60 % 60);
    s = Math.floor(allTime / 1000 % 60);
    if (m<10) {
      m='0'+m
    }
    if (s<10) {
      s='0'+s
    }
    t.setData({
      countdown: m + ":" + s,
    })
    setTimeout(t.countDown, 1000);
  } else {
    console.log('已截止')
    t.setData({
      countdown: '00:00'
    })
    wx.switchTab({
      url: '/pages/my/my',
    })
  }
},

  //取消订单
  gocancelorder(e){
    var t=this
    wx.showModal({
      title:'提示',
      content:'您将取消订单',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.Interfaceurl + 'cancelGoodsOrder',
            method: "post",
            header: {
                "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
            },
            data: {
                userId: app.globalData.userId,
                orderId:t.data.orderid,             
            },
            success: res => {
                console.log(res)
                wx.showToast({
                  title: '取消成功',
                })
                wx.reLaunch({
                  url: '/pages/my/my',
                })
                
            }
        })
  
  
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  //再支付
  againpay(){
    wx.request({
      url: app.globalData.Interfaceurl + 'againPreOrder',
      method: "post",
      header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
          userId: app.globalData.userId,
          orderId:this.data.goods.id
      },
      success: res => {
        if(res.data.result=='0'){
          wx.requestPayment({
            timeStamp: res.data.dataInfo.timeStamp,
            nonceStr: res.data.dataInfo.nonceStr,
            package: res.data.dataInfo.package,
            signType: res.data.dataInfo.signType,
            paySign: res.data.dataInfo.sign,
            success(res) { 
              console.log(res)
              
                wx.switchTab({
                    url: '/pages/my/my',
                })
            },
            fail(res) {
              wx.showToast({
                title: '您已经取消支付',
                icon:'none'
              })
            }
        })

        }else{
          wx.showToast({
            title: res.date.message,
            icon:'none'
          })
        }
        
          
      }
  })

  },
  //联系客服
  handleContact (e) {
    console.log(e.detail.path)
    console.log(e.detail.query)
  },

  getGoodsOrderDetails(id){
    var t=this
    wx.request({
      url: app.globalData.Interfaceurl + 'getGoodsOrderDetails',
      method: "post",
      header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
          userId: app.globalData.userId,
          orderId:id
      },
      success: res => {
          console.log(res)
          t.setData({
            goods:res.data.goodsOrder,
            isloadcomplete:false
          })
          t.countDown();
          console.log(t.data.goods)
          // if (myorderStats==''&&mypayStats=='') {
            
    
          // }
          
          
          
      }
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})